<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="河流名称" prop="riverName">
        <el-input
          v-model="queryParams.riverName"
          placeholder="请输入河流名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['riv:event:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['riv:event:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['riv:event:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['riv:event:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="eventList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="事件标题" align="center" prop="title" />
      <el-table-column label="河流名称" align="center" prop="riverName" />
      <el-table-column label="河长名称" align="center" prop="userName" />
      <el-table-column label="上报人名称" align="center" prop="leaderName" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.river_event_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="事件级别" align="center" prop="level">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.river_patrol_level" :value="scope.row.level"/>
        </template>
      </el-table-column>

      <el-table-column label="图片" align="center" prop="img" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.img" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="处理状态" align="center" prop="resultStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.river_patrol_solve" :value="scope.row.resultStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="处理结果" align="center" prop="resultContent" />
      <el-table-column label="处理结果图例" align="center" prop="resultImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.resultImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['riv:event:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['riv:event:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改河流事件对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="河流名称" prop="riverId">
          <el-select v-model="form.riverId" placeholder="请选择河流名称" @change="riverChange" >
            <el-option
              v-for="dict in riverList"
              :key="dict.riverId"
              :label="dict.riverName"
              :value="parseInt(dict.riverId)"
            />
          </el-select>
        </el-form-item>



        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.river_event_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="事件级别" prop="level">
          <el-radio-group v-model="form.level">
            <el-radio
              v-for="dict in dict.type.river_patrol_level"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>


        <el-form-item label="事件标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入事件标题" />
        </el-form-item>

        <el-form-item label="事件日期" prop="eventTime">
          <el-date-picker
            v-model="form.eventTime"
            type="date"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="事件内容描述" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="图片" prop="img">
          <image-upload v-model="form.img"/>
        </el-form-item>

        <baidu-map class="river-map" :center="{lng: 105.9409, lat: 29.3722}" :zoom="15" @click="getPoint">
          <bm-marker :position="point" :dragging="true" animation="BMAP_ANIMATION_BOUNCE"></bm-marker>
        </baidu-map>

        <el-form-item label="处理状态" prop="resultStatus">
          <el-radio-group v-model="form.resultStatus">
            <el-radio
              v-for="dict in dict.type.river_patrol_solve"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="处理结果" prop="resultContent">
          <el-input v-model="form.resultContent" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="处理结果图例" prop="resultImg">
          <image-upload v-model="form.resultImg"/>
        </el-form-item>





      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { listEvent, getEvent, delEvent, addEvent, updateEvent } from "@/api/riv/event";
import {listInfo} from "@/api/riv/info";


export default {
  name: "Event",
  dicts: ['river_patrol_level', 'river_event_type', 'river_patrol_solve'],
  data() {
    return {
      point:{lng: 105.9409, lat: 29.3722},
      polygonPath:"",
      riverList:{},
      riverName: "",
      leaderId: "",
      leaderName: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 河流事件表格数据
      eventList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        riverName: null,
        leaderName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        eventId: [
          { required: true, message: "事件ID不能为空", trigger: "blur" }
        ],
        riverId: [
          { required: true, message: "河流ID不能为空", trigger: "blur" }
        ],
        riverName: [
          { required: true, message: "河流名称不能为空", trigger: "change" }
        ],
        type: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        level: [
          { required: true, message: "事件级别不能为空", trigger: "change" }
        ],
        resultStatus: [
          { required: true, message: "处理状态不能为空", trigger: "change" }
        ],
        title: [
          { required: true, message: "标题不能为空", trigger: "change" }
        ],
        content: [
          { required: true, message: "内容不能为空", trigger: "change" }
        ],
        eventTime: [
          { required: true, message: "日期不能为空", trigger: "change" }
        ]
      },
    };
  },
  created() {
    this.getList();
    this.getRiver();
  },
  methods: {
    getPoint(e){
      this.point.lng = e.point.lng
      this.point.lat = e.point.lat
    },
    //查询河流name
    riverChange(r){
      this.riverList.forEach(item => {
        if(item.riverId == r){
          this.riverName = item.riverName
          this.userId = item.userId
          this.userName = item.userName
        }
      })
    },
    /** 查询河流事件列表 */
    getList() {
      this.loading = true;
      listEvent(this.queryParams).then(response => {
        this.eventList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询河流名称 */
    getRiver() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.riverList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        eventId: null,
        riverId: null,
        riverName: null,
        leaderId: null,
        // leaderName: null,
        userId: null,
        userName: null,
        type: null,
        level: null,
        title: null,
        content: null,
        coordinate: null,
        eventTime: null,
        img: null,
        resultStatus: null,
        resultContent: null,
        resultImg: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.eventId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加河流事件";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();

      this.point = JSON.parse(row.coordinate)

      const eventId = row.eventId || this.ids
      getEvent(eventId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改河流事件";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {

          this.form.riverName=this.riverName
          this.form.userId=this.userId
          this.form.userName=this.userName


          if(this.form.leaderId == null || this.form.leaderId === ""){
            this.form.leaderId = this.$store.state.user.id;
            this.form.leaderName = this.$store.state.user.name;
          }else{
            this.form.updateId = this.$store.state.user.id;
            this.form.updateUser = this.$store.state.user.name;
          }

          this.form.coordinate = JSON.stringify(this.point);

          if (this.form.eventId != null) {
            updateEvent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEvent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const eventIds = row.eventId || this.ids;
      this.$modal.confirm('是否确认删除河流事件编号为"' + eventIds + '"的数据项？').then(function() {
        return delEvent(eventIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('riv/event/export', {
        ...this.queryParams
      }, `event_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.river-map{
  width: 100%;
  height: 400px;
}
</style>
